package com.greenant.store.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.greenant.store.R;
import com.greenant.store.bean.OrderItem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public final class  OrdersInfoListAdapter extends BaseAdapter {
	private Context context;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private DisplayImageOptions options;
	private List<OrderItem> list = new ArrayList<OrderItem>();
	private String uri;
		
	OrdersInfoListAdapter(Context context) {
				
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.good_default)
				.showImageForEmptyUri(R.drawable.good_default)
				.showImageOnFail(R.drawable.good_default)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();
	}
	
	public static OrdersInfoListAdapter newInstance(Context context,List<OrderItem> items)	{
		
		OrdersInfoListAdapter adapter=new OrdersInfoListAdapter(context);
		adapter.context = context;
		adapter.list = items;		
		return adapter;
	}


	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		DecimalFormat    df   = new DecimalFormat("######0.00");    
		OrderItem hh = list.get(position);
		H h = null;
		if(view==null){
			h = new H();
			view = LayoutInflater.from(context).inflate(R.layout.item_order_info, parent, false);
			h.image = (ImageView)view.findViewById(R.id.ivImage);
			h.name = (TextView)view.findViewById(R.id.txGoodsName);
			h.price = (TextView)view.findViewById(R.id.txPrice);
			h.quantity = (TextView)view.findViewById(R.id.txGoodsCounts);
			h.weight = (TextView)view.findViewById(R.id.txWeight);
			h.cost = (TextView)view.findViewById(R.id.txCost);
			view.setTag(h); 
		}else{
			h = (H)view.getTag();
		}		
		
	    ImageLoader.getInstance().displayImage(hh.getPicUrl(), h.image, options, animateFirstListener);
		h.name.setText(hh.getName());
		h.price.setText("¥"+hh.getPrice()+"/"+hh.getUnit());
		
		if (hh.isWeighAble()){
			h.quantity.setText("数量:"+hh.getQuantity()+hh.getWeighUnit());
			h.weight.setText("称重:"+hh.getWeight()+"斤");
			h.cost.setText("小计:¥"+df.format(hh.getWeight()*hh.getPrice()));
		}
		else{
			h.quantity.setText("数量:"+hh.getQuantity()+hh.getUnit());
			h.cost.setText("小计:¥"+hh.getQuantity()*hh.getPrice());
		}
		
		return view;
	}

	class H{
		ImageView image;
		TextView name;
		TextView price;
		TextView quantity;
		TextView weight;
		TextView cost;
	}
	public void showMessage(final String message) {
        Toast.makeText(this.context, message, Toast.LENGTH_LONG).show();
    }

    public void showMessage(final String text, Exception e, boolean busy) {
        if (e == null) {
            showMessage(text + " finished.");
        } else {
            showMessage(e.toString());
        }
    }
    
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
